package io.renren.modules.app.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.EarningsRecordingService;
import io.renren.modules.app.service.OrdersService;
import io.renren.modules.app.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.MemberPurseEntity;
import io.renren.modules.app.service.MemberPurseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 会员钱包表
 *
 * @date 2022-04-20 22:37:44
 */
@RestController
@RequestMapping("app/memberpurse")
public class MemberPurseController {

    @Autowired
    private MemberPurseService memberPurseService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private EarningsRecordingService earningsRecordingService;

    @Autowired
    private UserService userService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:memberpurse:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberPurseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{purseId}")
    @RequiresPermissions("generator:memberpurse:info")
    public R info(@PathVariable("purseId") Long purseId) {
        MemberPurseEntity memberPurse = memberPurseService.getById(purseId);

        return R.ok().put("memberPurse", memberPurse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:memberpurse:save")
    public R save(@RequestBody MemberPurseEntity memberPurse) {
        memberPurse.setCreateTime(new Date());
        memberPurseService.save(memberPurse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:memberpurse:update")
    public R update(@RequestBody MemberPurseEntity memberPurse) {
        memberPurseService.updateById(memberPurse);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:memberpurse:delete")
    public R delete(@RequestBody Long[] purseIds) {
        memberPurseService.removeByIds(Arrays.asList(purseIds));

        return R.ok();
    }

    /**
     * 获取用户钱包
     *
     * @param userId 用户id
     * @return
     */
    @Login
    @GetMapping("/queryUserPurse/{userId}")
    public R queryUserPurse(@PathVariable("userId") Long userId) {
        MemberPurseEntity memberPurse = memberPurseService.queryUserPurse(userId);
        BigDecimal totalRevenue = earningsRecordingService.queryUserTotalRevenue(userId);
        memberPurse.setTotalRevenue(totalRevenue);
        return R.ok().put("memberPurse", memberPurse);
    }

    /**
     * 获取用户余额or待返金额
     *
     * @param userId 用户id
     * @return
     */
    @Login
    @GetMapping("/queryUserBalanceOrRefunded/{userId}")
    public R queryUserBalanceOrRefunded(@PathVariable("userId") Long userId) {
        MemberPurseEntity memberPurse = memberPurseService.queryUserPurse(userId);
        BigDecimal treatRefundedMoney = ordersService.statisticsAmountToBeRefunded(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("balance", memberPurse.getWithdrawalsAmount());
        map.put("treatRefundedMoney", treatRefundedMoney);
        return R.ok().put("data", map);
    }


    /**
     * 查询今日佣金、今日完成票数、每天票数、当前余额、 待返金额
     *
     * @param userId 用户id
     * @return
     */
    @Login
    @GetMapping("/getGrabVotesData/{userId}")
    public R getGrabVotesData(@PathVariable("userId") Long userId) {
        //今日佣金
        BigDecimal todayIncomeAmount = earningsRecordingService.queryUserTodayIncomeAmount(userId);
        //今日完成票数
        Integer todayFinishOrder = ordersService.getTodayFinishOrder(userId);

        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(userId);
        //每天票数
        UserEntity user = userService.queryUserDetails(userEntity);
        //余额
        MemberPurseEntity memberPurse = memberPurseService.queryUserPurse(userId);
        //待返金额
        BigDecimal treatRefundedMoney = ordersService.statisticsAmountToBeRefunded(userId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("todayCommission", todayIncomeAmount);
        map.put("todayCompletedOrders", todayFinishOrder);
        map.put("dailyVotes", user.getTakeOrdersFrequency());
        map.put("balance", memberPurse.getWithdrawalsAmount());
        map.put("treatRefundedMoney", treatRefundedMoney);
        return R.ok().put("data", map);
    }

}
